<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <title>Document</title>

    <!-- 自己写的css文件 -->
    <link rel="stylesheet" type="text/css" href="../css/background1.css">

    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <!-- 可选的Bootstrap主题文件（一般不使用） -->
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"></script>

    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <nav class="navbar navbar-default" role="navigation">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button> <a class="navbar-brand" href="/home">个人博客</a>
                </div>

                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <ul class="nav navbar-nav">
                        <li class="active">
                            <a href="/home">主页</a>
                        </li>

                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">博文<strong class="caret"></strong></a>
                            <ul class="dropdown-menu">
                                <li>
                                    <a href="/writingArticle">写博文</a>
                                </li>
                                <li class="divider">
                                </li>
                                <li>
                                    <a href="/articleList">文章列表</a>
                                </li>
                            </ul>
                        </li>

                        <li>
                            <a href="#">待开发</a>
                        </li>

                    </ul>
                    <form class="navbar-form navbar-left" role="search">
                        <div class="form-group">
                            <input type="text" class="form-control" />
                        </div> <button type="submit" class="btn btn-default">搜索（待开发）</button>
                    </form>
                    <ul class="nav navbar-nav navbar-right">
                        <li>
                            <a href="#">链接（待开发）</a>
                        </li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">我的账号<strong class="caret"></strong></a>
                            <ul class="dropdown-menu">
                                <li>
                                    <a href="#">待开发</a>
                                </li>
                                <li class="divider">
                                </li>
                                <li>
                                    <a href="/logout">登出账号</a>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </div>

            </nav>
        </div>
    </div>


    <div class="row clearfix">
        <div class="col-md-5 column">
            <!--<img alt="140x140" width="100%" height="auto" src="../img/baby.jpg" />-->
            <div id="myCarousel" class="carousel slide">
                <!-- 轮播（Carousel）指标 -->
                <ol class="carousel-indicators">
                    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
                    <li data-target="#myCarousel" data-slide-to="1"></li>
                    <li data-target="#myCarousel" data-slide-to="2"></li>
                </ol>
                <!-- 轮播（Carousel）项目 -->
                <div class="carousel-inner">
                    <div class="item active">
                        <img src="../img/baby.jpg" alt="First slide">
                    </div>
                    <div class="item">
                        <img src="../img/baby.jpg" alt="Second slide">
                    </div>
                    <div class="item">
                        <img src="../img/baby.jpg" alt="Third slide">
                    </div>
                </div>
                <!-- 轮播（Carousel）导航 -->
                <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
                    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                    <span class="sr-only">Previous</span>
                </a>
                <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
                    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                    <span class="sr-only">Next</span>
                </a>
            </div>
        </div>
        <div class="col-md-7 column">
            <div class="jumbotron well">
                <h1>
                    你好，访问者！！
                </h1>
                <p></p>
                <p>
                    该小项目主要用SSM（后台）和bootstrap（前端）搭建。因为其他功能还不确定，所以暂时只开发了<strong>博文</strong>相关的功能，其他功能会在后面一步一步补充的，抱歉！                </p>
                <p>
                    <a class="btn btn-primary btn-large" href="#">Learn more</a>
                </p>
            </div>
        </div>
    </div>
    <div class="clearfix panel panel-default panel-footer">
        <div class="col-md-4 column">
            <h2 id="heading1">
                Heading1
            </h2>
            <p id="profile1">
                Profile1
            </p>
            <p>
                <a id="a1" class="btn" style="display:block; text-align:right; float:right;" href="#" onclick="showArticle()">View details »</a>
            </p>
        </div>
        <div class="col-md-4 column">
            <h2 id="heading2">
                Heading2
            </h2>
            <p id="profile2">
                Profile2
            </p>
            <p>
                <a id="a2" class="btn" style="display:block; text-align:right; float:right;" href="#" onclick="showArticle()">View details »</a>
            </p>
        </div>
        <div class="col-md-4 column">
            <h2 id="heading3">
                Heading3
            </h2>
            <p id="profile3">
                Profile3
            </p>
            <p>
                <a id="a3" class="btn" style="display:block; text-align:right; float:right;" href="#" onclick="showArticle()">View details »</a>
            </p>
        </div>
    </div>
</div>

<script>

    //初始化方法，这里主要用来获取方法
    $(document).ready(function (){
        $.ajax({
            type: "post",
            // ..代表的应该是 http://localhost:8080
            url: "../getNewestArticles",
            dataType: "json",
            async:false,

            // //key/value格式
            // data: 'username=ha&password=123',
            success: function(data){
                theArticles = data;  //接收三篇文章的数据（全局变量）
                assignAbstract(data);

                console.log(theArticles);
                console.log(data);
                console.log(data[0]);
                console.log(data[0].author);

            }
        });
    });

    //赋予几篇文章简述以内容
    function assignAbstract(data) {
        $("#heading1").text(data[0].title);
        $("#heading2").text(data[1].title);
        $("#heading3").text(data[2].title);

        $("#profile1").text(processDetails(data[0].details));
        $("#profile2").text(processDetails(data[1].details));
        $("#profile3").text(processDetails(data[2].details));
    }

    //显示一部分内容
    function processDetails(data) {
        var strLength = 60; //设置显示的字符串长度上限
        var profile;

        if (data.length > strLength){
            profile = data.substr(0, strLength) + "……";
        }else {
            profile = data;
        }
        return profile;
    }

    //跳转到另一个页面去显示文章
    //获取是哪个a标签，即可知道要打开的是哪篇文章
    $(document).click(function(e){
        a_order = $(e.target).attr("id");
        console.log(a_order);

        var id = 0;
        if(a_order == "a1"){
            id = theArticles[0].id;
        }else if (a_order == "a2"){
            id = theArticles[1].id;
        }else if (a_order == "a3"){
            id = theArticles[2].id;
        }

        if (id != 0){
            //接在?后面是为了传递值
            window.location.href="../readingArticle?id="+ id ;
        }
    })


</script>

</body>
</html>